﻿using System;
using System.Web.UI.WebControls;
using Module.Core.LoginSystem.Commons;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.LoginSystem.Commons.Utilities;
using Module.Core.LoginSystem.Components.Modules;

namespace Web.Cms.Modules
{
    public partial class ModuleDefinitionManagement : AdminUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnDelete.Attributes.Add("onclick", MiscUtility.MSG_DELETE_CONFIRM);
            }
        }

        protected void dtgModuleDefinitions_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                //Load Module Definition
                int _moduleDefId = ConvertUtility.ToInt32(dtgModuleDefinitions.SelectedValue);
                ModuleDefinitionInfo _moduleDef = ModuleDefinitionController.GetModuleDefinition(_moduleDefId);
                txtModuleDefID.Text = _moduleDef.ModuleDefID.ToString();
                txtModuleDefTitle.Text = _moduleDef.Title;
                txtModuleDefDes.Text = _moduleDef.Description;
                txtModuleDefFolder.Text = _moduleDef.Folder;
                lblModuleDefinition.Text = _moduleDef.Title;
                pnlModules.Visible = true;

            }
            finally { }
        }

        protected void dtgModules_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = Convert.ToString(e.Row.RowIndex + 1 +
                                      dtgModules.PageIndex * dtgModules.PageSize);
                Button btnDeleteControl = (Button)e.Row.FindControl("btnDeleteControl");
                btnDeleteControl.Attributes.Add("onclick", MiscUtility.MSG_DELETE_CONFIRM);
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                int _moduleDefId = ConvertUtility.ToInt32(txtModuleDefID.Text);
                ModuleDefinitionInfo _moduleDef = ModuleDefinitionController.GetModuleDefinition(_moduleDefId);
                _moduleDef.Title = txtModuleDefTitle.Text;
                _moduleDef.Description = txtModuleDefDes.Text;
                _moduleDef.Folder = txtModuleDefFolder.Text;
                ModuleDefinitionController.UpdateModuleDefinition(_moduleDef);
            }
            finally
            {

            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                ModuleDefinitionInfo _moduleDef = new ModuleDefinitionInfo();
                _moduleDef.Title = txtModuleDefTitle.Text;
                _moduleDef.Description = txtModuleDefDes.Text;
                _moduleDef.Folder = txtModuleDefFolder.Text;
                _moduleDef.ModuleDefID = ModuleDefinitionController.CreateModuleDefinition(_moduleDef);
                ModuleDefinitionController.AddModuleDefinitionToPortal(_moduleDef.ModuleDefID, CurrentPortalID);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int _moduleDefId = ConvertUtility.ToInt32(txtModuleDefID.Text);
                ModuleDefinitionController.DeleteModuleDefinition(_moduleDefId);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            //Load Module Definitions
            dtgModuleDefinitions.DataSource = ModuleDefinitionController.GetModuleDefinitions(CurrentPortalID);
            dtgModuleDefinitions.RowDataBound += new GridViewRowEventHandler(dtgModuleDefinitions_RowDataBound);
            dtgModuleDefinitions.DataBind();
            //Load Module Control
            int _moduleDefId = ConvertUtility.ToInt32(dtgModuleDefinitions.SelectedValue);
            if (_moduleDefId == 0) return;
            dtgModules.DataSource = ModuleController.GetModules(_moduleDefId);
            dtgModules.RowDataBound += new GridViewRowEventHandler(dtgModules_RowDataBound);
            dtgModules.DataBind();
            //Load Module Detail
        }
        protected static void dtgModuleDefinitions_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = Convert.ToString(e.Row.RowIndex + 1);
            }
        }

        protected void dtgModules_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int _curModuleControlId = ConvertUtility.ToInt32(e.CommandArgument);
            switch (e.CommandName)
            {
                case "editmodulecontrol":
                    Response.Redirect(
                        UIProcess.GetCPPage(CurrentPortalID, "sys_modulecontroledit", new string[] { "moduleid" },
                                            new string[] { _curModuleControlId.ToString() }));
                    break;
                case "deletemodulecontrol":
                    try
                    {
                        ModuleController.DeleteModule(_curModuleControlId);
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                    break;
                default:
                    break;
            }
        }

        protected void btnAddModuleControl_Click(object sender, EventArgs e)
        {
            Response.Redirect(
                UIProcess.GetCPPage(CurrentPortalID, "sys_modulecontroledit", new string[] { "moduledefid" },
                                    new string[] { txtModuleDefID.Text }));
        }

        protected void dtgModules_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            dtgModules.PageIndex = e.NewPageIndex;
            dtgModules.DataBind();
        }

        protected void dtgModuleDefinitions_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            dtgModuleDefinitions.PageIndex = e.NewPageIndex;
        }
    }
}